package com.example.service.webhook.business.security.repository;

import com.example.service.webhook.business.carrier.entity.MobileVoiceCallEntity;
import com.example.service.webhook.business.security.entity.InsuranceRecordEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

/**
 * 社保
 * ClassName: EducationRepository    
 * date: 2016年7月26日 下午5:35:38   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class InsuranceRecordRepository extends BaseDAO<InsuranceRecordEntity>{
	
	@Resource(name="templateSecurity")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
    public void saveInsuranceRecordEntity(InsuranceRecordEntity entity) throws Exception  {
		entity.setCreateTime(DateUtil.getCurrentDate());
		entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.add(entity);
    }
    
    public void deleteInsuranceRecordEntity(String userId,String userInfoId){
    	String sql = "delete from t_insurance_record where userId=? and userInfoId=?";
    	this.delete(sql, new Object[]{userId,userInfoId});
    }


	public int[] batchSave(String userId, final List<InsuranceRecordEntity> calls) {

		String sql="INSERT INTO t_insurance_record (userId,userInfoId,areaCode,amount,baseNumber,corporationName,corporationPayment,corporationScale,personalPayment,customerScale,dealTime,description,insuranceType,month,monthEnd,status,createTime,lastModifyTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		final java.sql.Timestamp d =  new java.sql.Timestamp(DateUtil.getCurrentDate().getTime());

		int[] updateCounts = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				InsuranceRecordEntity entity = calls.get(i);
				ps.setString(1, entity.getUserId());
				ps.setString(2, entity.getUserInfoId());
				ps.setString(3, entity.getAreaCode());
				if(entity.getAmount()!=null){
					ps.setInt(4, entity.getAmount());
				}else{
					ps.setNull(4, Types.INTEGER);
				}
				if(entity.getAmount()!=null){
					ps.setInt(5, entity.getBaseNumber());
				}else{
					ps.setNull(5, Types.INTEGER);
				}

				ps.setString(6, entity.getCorporationName());
				if(entity.getAmount()!=null){
					ps.setInt(7, entity.getBaseNumber());
				}else{
					ps.setNull(7, Types.INTEGER);
				}
				ps.setString(8, entity.getCorporationScale());
				if(entity.getAmount()!=null){
					ps.setInt(9, entity.getPersonalPayment());
				}else{
					ps.setNull(9, Types.INTEGER);
				}
				ps.setString(10, entity.getCustomerScale());
				ps.setString(11, entity.getDealTime());
				ps.setString(12, entity.getDescription());
				ps.setString(13, entity.getInsuranceType());
				ps.setString(14, entity.getMonth());
				ps.setString(15, entity.getMonthEnd());
				ps.setString(16, entity.getStatus());
				ps.setTimestamp(17, d);
				ps.setTimestamp(18, d);

			}

			@Override
			public int getBatchSize() {
				return calls.size();
			}
		});
		return updateCounts;
	}
    
  
    
}
